const Koa = require('koa')
const app = new Koa()
const Router = require('koa-router')
let router = new Router()

const mongoose = require('mongoose')
const fs = require('fs')

router.get('/insertAllGoodsInfo', async (ctx) => {
    fs.readFile('./data_json/newGoods.json', 'utf8', (err, data) => {
        data = JSON.parse(data)
        let saveCount = 0
        const Goods = require('../database/schema/Goods')
        data.map((value, index) => {
            console.log(value)
            let newGoods = new Goods(value)
            newGoods.save().then(() => {
                saveCount++
                console.log('成功' + saveCount)
            }).catch(error => {
                console.log('失败：' + error)
            })
        })
    })
    ctx.body = "开始导入数据"
})

router.get('/insertAllCategory', async (ctx) => {
    fs.readFile('./data_json/category.json', 'utf8', (err, data) => {
        data = JSON.parse(data)
        let saveCount = 0
        const Category = require('../database/schema/Category')
        data.RECORDS.map((value, index) => {
            console.log(value)
            let newCategory = new Category(value)
            newCategory.save().then(() => {
                saveCount++
                console.log('成功' + saveCount)
            }).catch(error => {
                console.log('失败：' + error)
            })
        })
    })
    ctx.body = "开始导入数据"
})

router.get('/insertAllCategorySub', async (ctx) => {
    fs.readFile('./data_json/category_sub.json', 'utf8', (err, data) => {
        data = JSON.parse(data)
        let saveCount = 0
        const CategorySub = require('../database/schema/CategorySub')
        data.RECORDS.map((value, index) => {
            console.log(value)
            let newCategorySub = new CategorySub(value)
            newCategorySub.save().then(() => {
                saveCount++
                console.log('成功插入' + saveCount)
            }).catch(error => {
                console.log('插入失败:' + error)
            })
        })
    })
    ctx.body = "开始导入数据"
})

router.post('/getDetailGoodsInfo', async ctx => {
    try {
        let goodsId = ctx.request.body.goodsId
        const goods = require('../database/schema/Goods')
        let result = await goods.findOne({ ID: goodsId }).exec()
        ctx.body = {
            code: 200,
            data: result,
            message: 'ok'
        }
    } catch (error) {
        ctx.body = {
            code: 200,
            message: error
        }
    }
})

router.get('/getCategoryList', async ctx => {
    try {
        const category = require('../database/schema/Category')
        let result = await category.find().exec()
        ctx.body = {
            code: 200,
            data: result,
            message: 'ok'
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            message: error
        }
    }
})

router.post('/getCategorySubList', async (ctx) => {
    try {
        let categoryId = ctx.request.body.categoryId
        const CategorySub = require('../database/schema/CategorySub')
        let result = await CategorySub.find({ MALL_CATEGORY_ID: categoryId }).exec()
        ctx.body = {
            code: 200,
            data: result,
            message: 'ok'
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            message: error
        }
    }

})

router.post('/getGoodsListByCategorySubID', async ctx => {
    try {
        let categorySubId = ctx.request.body.categorySubId
        let page = ctx.request.body.page
        let num = 10
        let start = (page - 1) * num
        const goods = require('../database/schema/Goods')
        let result = await goods.find({ SUB_ID: categorySubId }).skip(start).limit(num).exec()
        ctx.body = {
            code: 200,
            data: result,
            message: 'ok'
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            message: error
        }
    }
})

router.get('/getRecommendGoodsList', async ctx => {
    try {
        const goods = require('../database/schema/Goods')
        let result = await goods.find().limit(18).exec()
        ctx.body = {
            code: 200,
            data: result,
            message: 'ok'
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            message: error
        }
    }
})

router.get('/getHotGoodsList', async ctx => {
    try {
        const goods = require('../database/schema/Goods')
        let result = await goods.find().skip(18).limit(20).exec()
        ctx.body = {
            code: 200,
            data: result,
            message: 'ok'
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            message: error
        }
    }
})

module.exports = router